package com.asl.website.legalDocuments.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.legalDocuments.domain.LegalDocumentDTO;
import com.asl.website.legalDocuments.domain.LegalDocumentTitleDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.legalDocuments.domain.LegalDocuments;
import com.asl.website.legalDocuments.service.ILegalDocumentsService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 法律文档Controller
 * 
 * @author ruoyi
 * @date 2025-06-02
 */
@RestController
@RequestMapping("/legalDocuments/legalDocuments")
public class LegalDocumentsController extends BaseController
{
    @Autowired
    private ILegalDocumentsService legalDocumentsService;
    /**
     * 公共接口：获取所有法律文档的标题列表（根据语言）
     */
    @GetMapping("/public/titles")
    public AjaxResult getAllLegalTitles(
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        List<LegalDocuments> docs = legalDocumentsService.selectLegalDocumentsList(new LegalDocuments());
        if (docs.isEmpty()) {
            return AjaxResult.error("暂无法律文档");
        }

        List<LegalDocumentTitleDTO> result = docs.stream()
                .map(doc -> new LegalDocumentTitleDTO(doc, acceptLanguage))
                .sorted(Comparator.comparingLong(LegalDocumentTitleDTO::getId))
                .collect(Collectors.toList());

        return AjaxResult.success(result);
    }

    /**
     * 公共接口：根据ID获取法律文档详情（根据语言）
     */
    @GetMapping("/public/{id}")
    public AjaxResult getLegalDocumentById(
            @PathVariable("id") Long id,
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        LegalDocuments doc = legalDocumentsService.selectLegalDocumentsById(id);
        if (doc == null) {
            return AjaxResult.error("法律文档不存在");
        }

        LegalDocumentDTO dto = new LegalDocumentDTO(doc, acceptLanguage);
        return AjaxResult.success(dto);
    }
    /**
     * 查询法律文档列表
     */
    @PreAuthorize("@ss.hasPermi('legalDocuments:legalDocuments:list')")
    @GetMapping("/list")
    public TableDataInfo list(LegalDocuments legalDocuments)
    {
        startPage();
        List<LegalDocuments> list = legalDocumentsService.selectLegalDocumentsList(legalDocuments);
        return getDataTable(list);
    }

    /**
     * 导出法律文档列表
     */
    @PreAuthorize("@ss.hasPermi('legalDocuments:legalDocuments:export')")
    @Log(title = "法律文档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LegalDocuments legalDocuments)
    {
        List<LegalDocuments> list = legalDocumentsService.selectLegalDocumentsList(legalDocuments);
        ExcelUtil<LegalDocuments> util = new ExcelUtil<LegalDocuments>(LegalDocuments.class);
        util.exportExcel(response, list, "法律文档数据");
    }

    /**
     * 获取法律文档详细信息
     */
    @PreAuthorize("@ss.hasPermi('legalDocuments:legalDocuments:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(legalDocumentsService.selectLegalDocumentsById(id));
    }

    /**
     * 新增法律文档
     */
    @PreAuthorize("@ss.hasPermi('legalDocuments:legalDocuments:add')")
    @Log(title = "法律文档", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LegalDocuments legalDocuments)
    {
        return toAjax(legalDocumentsService.insertLegalDocuments(legalDocuments));
    }

    /**
     * 修改法律文档
     */
    @PreAuthorize("@ss.hasPermi('legalDocuments:legalDocuments:edit')")
    @Log(title = "法律文档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LegalDocuments legalDocuments)
    {
        return toAjax(legalDocumentsService.updateLegalDocuments(legalDocuments));
    }

    /**
     * 删除法律文档
     */
    @PreAuthorize("@ss.hasPermi('legalDocuments:legalDocuments:remove')")
    @Log(title = "法律文档", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(legalDocumentsService.deleteLegalDocumentsByIds(ids));
    }
}
